<?php

namespace app\common\library;

use Alipay\EasySDK\Kernel\Config;
use Alipay\EasySDK\Kernel\Factory;
use app\common\model\Config as Model;

class Ali
{

    public static function app()
    {
        Factory::setOptions(self::getOptions());
        return Factory::payment();
    }

    public static function getOptions()
    {
        $config = Model::where('group', 'alipay')->column('value', 'name');
        $options = new Config();
        $options->protocol = 'https';
        $options->gatewayHost = 'openapi.alipay.com';
        $options->signType = 'RSA2';

        $options->appId = $config['ali_appid'];

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        $options->merchantPrivateKey = $config['merchantPrivateKey'];

        $options->alipayCertPath = ROOT_PATH . "public" . $config['ali_cert_path'];
        $options->alipayRootCertPath = ROOT_PATH . "public" . $config['ali_rootcert_path'];
        $options->merchantCertPath = ROOT_PATH . "public" . $config['ali_merchantcert_path'];

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        // $options->alipayPublicKey = '<-- 请填写您的支付宝公钥，例如：MIIBIjANBg... -->';
        //可设置异步通知接收服务地址（可选）
        //如果需要使用文件上传接口，请不要设置该参数
        $options->notifyUrl = request()->domain() . "/api/notify/ali_pay";

        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        $options->encryptKey = $config['aes_encryptkey'];
        return $options;
    }

    public static function util()
    {
        Factory::setOptions(self::getOptions());
        return Factory::util();
    }
}